﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AgLux
{
    /// <summary>
    /// Represents a control that displays an answer.
    /// </summary>

    public class Answer : Control
    {
        public event EventHandler Marked;
        public event EventHandler Unmarked;

        private Canvas _answer, _button;
        private Shape _circle;
        private TextBlock _text;
        private bool _isCorrect, _isMarked, _isMarkable;

        /// <summary>
        /// Initializes a new instance of the Answer class.
        /// </summary>

        /// <param name="text">
        /// The answer text.
        /// </param>

        /// <param name="isCorrect">
        /// Whether the answer is correct.
        /// </param>

        public Answer(string text, bool isCorrect)
            : base()
        {
            FrameworkElement resource = InitializeFromXaml(new StreamReader(GetType().Assembly.GetManifestResourceStream("eLearning.Answer.xaml")).ReadToEnd());

            _answer = (Canvas)resource.FindName("answer");
            _button = (Canvas)resource.FindName("button");
            _circle = (Shape)resource.FindName("circle");
            _text = (TextBlock)resource.FindName("text");

            _isCorrect = isCorrect;
            _isMarked = false;
            _isMarkable = true;

            _answer.MouseLeftButtonDown += new MouseEventHandler(BePressed);
            _text.Text = text;
            _text.SetValue(Canvas.LeftProperty, _button.Width + 5);
        }

        /// <summary>
        /// Prepare to display the answer.
        /// </summary>

        /// <param name="width">
        /// The width of the canvas on which the answer will be displayed.
        /// </param>

        public void Prepare(double width)
        {
            Width = _answer.Width = width;
            _text.Width = width - _button.Width - 5;
            Height = _answer.Height = _text.Height = _text.ActualHeight;
            _button.SetValue(Canvas.TopProperty, Height / 2 - _button.Height / 2);
        }

        /// <summary>
        /// Unmarks the answer.
        /// </summary>

        public void Unmark()
        {
            if (_isMarkable)
            {
                _circle.Fill = new SolidColorBrush(Colors.Black);
                _text.Foreground = new SolidColorBrush(Colors.Black);
                _isMarked = false;
            }
        }

        /// <summary>
        /// Corrects the answer.
        /// </summary>

        /// <returns>
        /// Whether the answer is marked and correct.
        /// </returns>

        public bool Correct()
        {
            bool markedCorrect = false;
            _isMarkable = false;

            if (_isMarked)
            {
                if (_isCorrect)
                {
                    _circle.Fill = new SolidColorBrush(Colors.Green);
                    _text.Foreground = new SolidColorBrush(Colors.Green);
                    markedCorrect = true;
                }

                else
                {
                    _circle.Fill = new SolidColorBrush(Colors.Red);
                    _text.Foreground = new SolidColorBrush(Colors.Red);
                }
            }

            return markedCorrect;
        }

        /// <summary>
        /// Occurs when the control is pressed.
        /// </summary>

        /// <param name="sender">
        /// The object that raised the event.
        /// </param>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        private void BePressed(object sender, EventArgs args)
        {
            if (_isMarked)
            {
                Unmark();
                OnUnmarked(new EventArgs());
            }

            else if (_isMarkable)
            {
                _circle.Fill = new SolidColorBrush(Colors.Blue);
                _text.Foreground = new SolidColorBrush(Colors.Blue);
                _isMarked = true;
                OnMarked(new EventArgs());
            }
        }

        /// <summary>
        /// Raises the Marked event.
        /// </summary>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        private void OnMarked(EventArgs args)
        {
            if (Marked != null)
            {
                Marked(this, args);
            }
        }

        /// <summary>
        /// Raises the Unmarked event.
        /// </summary>

        /// <param name="args">
        /// The EventArgs that contains the event data.
        /// </param>

        private void OnUnmarked(EventArgs args)
        {
            if (Unmarked != null)
            {
                Unmarked(this, args);
            }
        }
    }
}
